//
//  MXTucaoView.m
//  CatchBear
//
//  Created by 王启颖 on 2019/1/25.
//  Copyright © 2019 王启颖. All rights reserved.
//

#import "MXTucaoView.h"
#import "XMTextView.h"

@interface MXTucaoView ()
@property(nonatomic,strong) UIButton *tucaoBtn;
@property (strong, nonatomic) NSMutableDictionary *dicParams;
@property (strong, nonatomic) XMTextView *tv;
@property (copy, nonatomic) NSString *tv_str;
@end

@implementation MXTucaoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initCustomerControls];
    }
    return self;
}

- (void)initCustomerControls {
    self.backgroundColor = MX_GRAY_COLOR;
    self.tv = [[XMTextView alloc] initWithFrame:CGRectMake(16, 10, kScreenWidth-2*16, 200)];
    _tv.textFont = [UIFont systemFontOfSize:12];
    _tv.placeholder = @"我会认真看您的吐槽，并且根据大家的吐槽决定这个app的未来发展，感谢您的支持，更多问题可以添加微信：klkxxy。";
    [self addSubview:_tv];

    self.tucaoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.tucaoBtn changeCornerRadius:5];
    self.tucaoBtn.frame = CGRectMake(16, _tv.height+_tv.mj_y+30, kScreenWidth - 32, 50);
    self.tucaoBtn.backgroundColor = MX_NOCLICK_COLOR;
    [self.tucaoBtn setTitle:@"吐槽一下" forState:UIControlStateNormal];
    [self.tucaoBtn addTarget:self action:@selector(tucaoClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.tucaoBtn];
    WEAKSELF
    _tv.textViewListening = ^(NSString *textViewStr) {
        weakSelf.tv_str = textViewStr;
        NSLog(@"tv监听输入的内容：%@",textViewStr);
        if (textViewStr.length != 0) {
            weakSelf.tucaoBtn.enabled = YES;
            weakSelf.tucaoBtn.backgroundColor = MX_MAIN_COLOR;
            [weakSelf.tucaoBtn setTitleColor:MX_SECONDARY_COLOR forState:UIControlStateNormal];
        }else {
            weakSelf.tucaoBtn.enabled = NO;
            weakSelf.tucaoBtn.backgroundColor = MX_NOCLICK_COLOR;
            [weakSelf.tucaoBtn setTitleColor:MX_WHITE_COLOR forState:UIControlStateNormal];
        }
    };
}

-(void)tucaoClick{
    [self addTucaoRequest];
}

- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    
    return _dicParams;
}

-(void)addTucaoRequest{
    self.dicParams[@"tucao_info"] = self.tv_str;
    self.dicParams[@"user_id"] = [MXUserMessageTool message].user_id;
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:addTucao params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        
        
        if (code == 0) {
            NSLog(@"%@",result);
            [SVProgressHUD showImage:NULL_IMAGE status:@"吐槽成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"账号密码错误"];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
}


@end
